package demo;

import java.util.LinkedList;

import Worm.Node;

public class Worm {
	private int dir;
	public static final int UP=-10;
	public static final int DOWN=10;
	public static final int LEFT=-1;
	public static final int RIGHT=1;
	
	LinkedList<Node> nodes = new LinkedList<Node>();
	public Worm(){
		nodes.add(new Node(3,9));
		nodes.add(new Node(4,9));
		nodes.add(new Node(5,9));
		nodes.add(new Node(5,10));
		nodes.add(new Node(5,11));
		nodes.add(new Node(6,11));
		nodes.add(new Node(7,11));
		this.dir=UP;
	}
	public Worm(LinkedList<Node> nodes){
		this.nodes.clear();
		this.nodes.addAll(nodes);
	}
	
	public void step(){
		Node head = this.nodes.getFirst();
		int i = head.getI()+dir/10;
		int j = head.getJ()+dir%10;
		Node head1 = new Node(i,j);
		this.nodes.addFirst(head1);
		this.nodes.removeLast();
	}
	
	public void step(int dir){
		if(this.dir+dir==0){
			throw new RuntimeException("不能掉头");
		}
		this.dir=dir;
		step();
	}
	
	public boolean contains(Node node){
		return this.nodes.contains(node);
	}
	
	
	
	
	
}
